package com.gzw.dms.base;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.gzw.dms.MyApplication;
import com.gzw.dms.Receiver.NetReceiver;
import com.gzw.dms.activity.LoginActivity;
import com.gzw.dms.bean.Constant;

public abstract class BaseActivity extends AppCompatActivity  {
    private View view;
    private long exitTime=0;
    NetReceiver mReceiver = new NetReceiver();
    IntentFilter mFilter = new IntentFilter();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (MyApplication.getFlag() == -1) {
            protectApp();
        } else {
            setContentView();
            initViews();
            initData();
            initEvents();
        }

        mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(mReceiver, mFilter);
    }

    protected abstract void setContentView();

    protected abstract void initViews();

    protected abstract void initData();

    protected abstract void initEvents();

    protected void protectApp() {
        Intent intent = new Intent();
        intent.setClass(this, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(Constant.KEY_PROTECT_APP, true);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(mReceiver);

        super.onDestroy();
    }


    protected boolean isNull(EditText et) {
        boolean b = false;
        if (isNull(et.getText().toString())) {
            b = true;
        }
        return b;
    }

    protected boolean isNull(TextView tv) {
        boolean b = false;
        if (isNull(tv.getText().toString())) {
            b = true;
        }
        return b;
    }

    protected boolean isNull(String str) {
        boolean b = false;
        if (str.trim() == null
                || str.trim().length() == 0) {
            b = true;
        }
        return b;
    }

    /**
     * 含有标题和内容和一个按钮的对话框
     **/
    protected AlertDialog showAlertDialog(String title, String message) {
        AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle(title)
                .setMessage(message)
                .setPositiveButton("确定", null).show();
        return alertDialog;
    }

    /**
     * 含有标题、内容、两个按钮的对话框
     **/
    protected AlertDialog showAlertDialog(String title, String message,
                                          String positiveText,
                                          DialogInterface.OnClickListener onPositiveClickListener,
                                          String negativeText,
                                          DialogInterface.OnClickListener onNegativeClickListener) {
        AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle(title)
                .setMessage(message)
                .setPositiveButton(positiveText, onPositiveClickListener)
                .setNegativeButton(negativeText, onNegativeClickListener)
                .show();
        return alertDialog;
    }

    /**
     * 含有标题、内容、图标、两个按钮的对话框
     **/
    protected AlertDialog showAlertDialog(String title, String message,
                                          int icon, String positiveText,
                                          DialogInterface.OnClickListener onPositiveClickListener,
                                          String negativeText,
                                          DialogInterface.OnClickListener onNegativeClickListener) {
        AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle(title)
                .setMessage(message).setIcon(icon)
                .setPositiveButton(positiveText, onPositiveClickListener)
                .setNegativeButton(negativeText, onNegativeClickListener)
                .show();
        return alertDialog;
    }


//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK
//                && event.getAction() == KeyEvent.ACTION_DOWN) {
//            if ((System.currentTimeMillis() - exitTime) > 2000) {
//                Toast.makeText(this, "再按一次退出程序。", Toast.LENGTH_SHORT).show();
//                exitTime = System.currentTimeMillis();
//            } else {
//                exitApp();
//            }
//            return true;
//        }
//        return super.onKeyDown(keyCode, event);
//    }
    protected void exitApp(){
        finish();
    }
}
